<?php
class Default_Form_ValidateTour {
	// save errors info
	protected $_messagesError = null;
	
	// save data after validate
	protected $_arrData;
	
	protected $_imageFlag = true;
	public function __construct($arrParam = array(),$options = null) {
	
		// check Category Name
	
		if($arrParam['action'] == 'add') {
			$options = array('table'=>'tours','field'=>'name');
		}else {
			if($arrParam['action'] == 'edit') {
				$clause = 'id !=' . $arrParam['id'];
				$options = array('table'=>'tours','field'=>'name', 'exclude' => $clause);
			}
		}
	
		$validator = new Zend_Validate();
	
		// check name
		$validator->addValidator(new Zend_Validate_NotEmpty(),true)  
	              ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true);
	
		if(!$validator->isValid($arrParam['name'])) {
			$message = $validator->getMessages();
			$this->_messagesError['name'] = 'Tour name: ' . current($message);
			$arrParam['name'] = '';
		}
	
	    // check order form
		$validator = new Zend_Validate();
	
		$validator->addValidator(new Zend_Validate_Int(), true)
		          ->addValidator(new Zend_Validate_GreaterThan(0), true);
	              
		if(!$validator->isValid($arrParam['order'])) {
			$message = $validator->getMessages();
			$this->_messagesError['order'] = 'order: ' . current($message);
			$arrParam['order'] = '';
		}
		
		//check time limit
		$validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		          
		if(!$validator->isValid($arrParam['time_limit'])) {
			$message = $validator->getMessages();
			$this->_messagesError['time_limit'] = 'Time limit: ' . current($message);
			$arrParam['time_limit'] = '';
		}
		
		// check time available
		$validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		          
		if(!$validator->isValid($arrParam['time_available'])) {
			$message = $validator->getMessages();
			$this->_messagesError['time_available'] = 'Time available: ' . current($message);
			$arrParam['time_available'] = '';
		}
		
		// check cost
		if(!empty($arrParam['cost'])) {
			$validator = new Zend_Validate();
			
			$validator->addValidator(new Zend_Validate_NotEmpty(),true)
			          ->addValidator(new Zend_Validate_Float(), true)
			          ->addValidator(new Zend_Validate_GreaterThan(0), true);
			if(!$validator->isValid($arrParam['cost'])) {
				$message = $validator->getMessages();
				$this->_messagesError['cost'] = 'Cost: ' . current($message);
				$arrParam['cost'] = '';
			}
		}
		
		// check medium
		$validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if(!$validator->isValid($arrParam['medium'])) {
			$message = $validator->getMessages();
			$this->_messagesError['medium'] = 'Medium: ' . current($message);
			$arrParam['medium'] = '';
		}
		
		// Kiem tra image Location Start
		$validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if(!$validator->isValid($arrParam['location_start'])) {
			$message = $validator->getMessages();
			$this->_messagesError['location_start'] = 'Location start: ' . current($message);
			$arrParam['location_start'] = '';
		}
		
		// Kiem tra image Location End
		$validator = new Zend_Validate();
		
		$validator->addValidator(new Zend_Validate_NotEmpty(),true);
		
		if(!$validator->isValid($arrParam['location_end'])) {
			$message = $validator->getMessages();
			$this->_messagesError['location_end'] = 'Location end: ' . current($message);
			$arrParam['location_end'] = '';
		}
		
		// Kiem tra image
		$upload   = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('picture_tour');
	    $fileName = $fileInfo['picture_tour']['name'];
	    
	    if($arrParam['action'] == 'add') {
		    if(!empty($fileName)){
			
				$upload->addValidator('Extension',true,array('jpg','gif','png'),'picture_tour');
				$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'picture_tour');
				if(!$upload->isValid('picture_tour')){
					$message = $upload->getMessages();
					$this->_messagesError['picture_tour'] = 'Picture: ' . current($message);
			
				}
		    }else {
		    	$this->_messagesError['picture_tour'] = 'Picture: can\'t be empty';
		    }
	    }
	    
	    if($arrParam['action'] == 'edit') {
	    	if(!empty($fileName)){
	    		$upload->addValidator('Extension',true,array('jpg','gif','png'),'picture_tour');
	    		$upload->addValidator('Size',true,array('min'=>'2KB','max'=>'1000KB'),'picture_tour');
	    		if(!$upload->isValid('picture_tour')){
	    			$message = $upload->getMessages();
	    			$this->_messagesError['picture_tour'] = 'Picture: ' . current($message);
	    				
	    		}
	    	}else{
	    		$this->_imageFlag = false;
	    	}
	    }
	    
	    // save data user type in the form
	    $this->_arrData = $arrParam;
	 
	}
	
	
	
	// check errors
	public function isError() {
		if(count($this->_messagesError) > 0) {
			return true;
		}else {
			return false;
		}
	}
	
	// get errors
	public function getMessageError() {
		return $this->_messagesError;
	}
	
	// get data
	public function getData() {
		return $this->_arrData;
	}
	
	// get image
    public function getImage() {
    	return $this->_imageFlag;
    } 
	
}